package com.example.controller;

import com.example.common.Pager;
import com.example.common.Result;
import com.example.common.StringUtil;
import com.example.service.SuperService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author 紫煦桉
 */
public class SuperController<T> {
    private SuperService<T> superService;

    @Autowired
    private void setSuperService(SuperService<T> superService) {
        this.superService = superService;
    }
    private final Logger logger = Logger.getLogger(SuperController.class);

    public Pager<T> list(T param, String page, String size) {
        Pager<T> pager = new Pager<>(StringUtil.parseInt(page), StringUtil.parseInt(size));
        logger.info(param);
        pager.setCondition(param);
        pager.setTotal(superService.findTotal(pager));
        pager.setRows(superService.findList(pager));
        return pager;
    }

    public Result add(T param) {
        Result result;
        String statusAdd = "新增";
        if (superService.add(param)) {
            result = printSuccess(statusAdd);
        } else {
            result = printDefault(statusAdd);
        }
        return result;
    }

    public Result edit(T param) {
        Result result;
        String statusEdit = "修改";
        if (superService.edit(param)) {
            result = printSuccess(statusEdit);
        } else {
            result = printDefault(statusEdit);
        }
        return result;
    }

    public Result delete(T param) {
        Result result;
        String statusDelete = "删除";
        if (superService.delete(param)) {
            result = printSuccess(statusDelete);
        } else {
            result = printDefault(statusDelete);
        }
        return result;
    }

    public Result printSuccess(String e) {
        return new Result(Boolean.TRUE, e + "成功");
    }

    public Result printDefault(String e) {
        return new Result(Boolean.FALSE, e + "失败");
    }
}
